package main

import (
	"context"
	"fmt"
	"net"
	"qweewq.tk/flyff/pgk/common/network"
	"qweewq.tk/flyff/pgk/netutil"
)

type TcpPackServer struct {
	network.DefaultEvHandle
	*network.TcpPackServe
}

func (self *TcpPackServer) ParserPackHeader(content []byte) (l int, err error) {
	return netutil.ParserPackHeader(content)
}

func (self *TcpPackServer) BuildPackData(data []byte) []byte {
	return netutil.BuildPackData(data)
}

func NewTcpPackServer(ctx context.Context, listener net.Listener) *TcpPackServer {
	e := &TcpPackServer{
	}
	tcp := network.NewTcpServe(ctx, listener, e)
	e.TcpPackServe = network.NewTcpPackServe(tcp, netutil.PackHeaderSize, e)
	return e
}

func (self *TcpPackServer) OnFinish(id int32) {
	fmt.Printf("处理协程 %d 结束.\n", id)
}

func (self *TcpPackServer) OnAccept(connId int32, conn net.Conn) {
	self.SendData(connId, []byte("hello!"))
}

func (self *TcpPackServer) OnError(err error) {
	fmt.Println(err)
}

func (self *TcpPackServer) OnReceived(connId int32, content []byte) error {
	self.SendData(connId, content)
	fmt.Printf("%s\n", content)
	return nil
}

func (self *TcpPackServer) OnClosed(connId int32) {
	fmt.Println(connId, "关闭了!")
}
